"""
#!/usr/bin/env python
# -*- coding:utf-8 -*-
@Project : All_Designer
@File : ViewImage.py
@Author : RuanYaoHuang
@Time : 2023/3/12 20:08

"""
import time

import numpy as np
from PyQt5.QtCore import QThread


class viewOnlyChannelThread(QThread):
    def __init__(self, rawData, fig, vieWidget, parent=None):
        super(viewOnlyChannelThread, self).__init__(parent)
        self.rawData = rawData
        self.vieWidget = vieWidget
        self.fig1 = fig
        self.is_running = False

    def run(self):
        self.is_running = True
        origin = np.fromfile(self.rawData, dtype='uint8')
        if origin.shape[0] == 1253376:
            raw_data = origin.reshape((1024, 1224))
            self.view_RGGB_function(raw_data)
        elif origin.shape[0] == 1880064:
            img_bytes = np.unpackbits(origin)  # 转换为2进制
            img_bytes_reshape = img_bytes.reshape(-1, 12)  # 重置2进制矩阵shape
            origin = np.dot(img_bytes_reshape, 2 ** np.arange(11, -1, -1))  # 将2进制矩阵还原成十进制
            raw_data = origin.reshape((1024, 1224))
            self.view_RGGB_function(raw_data)
        elif origin.shape[0] == 2506752:
            img_bytes = np.unpackbits(origin)
            img_bytes_reshape = img_bytes.reshape(-1, 16)
            origin = np.dot(img_bytes_reshape, 2 ** np.arange(15, -1, -1))
            raw_data = origin.reshape((1024, 1224))
            self.view_RGGB_function(raw_data)
        elif origin.shape[0] == 3760128:
            img_bytes = np.unpackbits(origin)
            img_bytes_reshape = img_bytes.reshape(-1, 24)
            origin = np.dot(img_bytes_reshape, 2 ** np.arange(23, -1, -1))
            raw_data = origin.reshape((1024, 1224))
            self.view_RGGB_function(raw_data)
        elif origin.shape[0] == 5013504:
            raw_data = origin.reshape((2048, 2448))
            self.view_4_function(raw_data)
        self.quit()
        self.wait()
    def view_RGGB_function(self, raw_data):
        # 提取红色通道
        red_data = np.zeros_like(raw_data)
        red_data[::2, ::2] = raw_data[::2, ::2]

        Rbyte_r = red_data.astype('uint8')
        Rbyte_g = np.zeros_like(red_data)
        Rbyte_b = np.zeros_like(red_data)
        Rbytergb = np.stack([Rbyte_r, Rbyte_g, Rbyte_b], axis=-1)

        self.ax1 = self.fig1.add_subplot(221)
        self.ax1.set_aspect('equal')
        self.ax1.set_axis_off()
        self.ax1.imshow(Rbytergb)
        print("读取了RGB图像")
        # 提取绿色通道
        green_even_data = np.zeros_like(raw_data)
        green_even_data[::2, 1::2] = raw_data[::2, 1::2]
        green_even_data[1::2, ::2] = raw_data[1::2, ::2]

        Grbyte_r = np.zeros_like(green_even_data)
        Grbyte_g = green_even_data.astype('uint8')
        Grbyte_b = np.zeros_like(green_even_data)
        Grbytergb = np.stack([Grbyte_r, Grbyte_g, Grbyte_b], axis=-1)
        self.ax1 = self.fig1.add_subplot(222)
        self.ax1.set_axis_off()
        self.ax1.imshow(Grbytergb)

        green_odd_data = np.zeros_like(raw_data)
        green_odd_data[1::2, 1::2] = raw_data[1::2, 1::2]
        Glbyte_r = np.zeros_like(green_odd_data)
        Glbyte_g = green_odd_data.astype('uint8')
        Glbyte_b = np.zeros_like(green_odd_data)
        Glbytergb = np.stack([Glbyte_r, Glbyte_g, Glbyte_b], axis=-1)
        self.ax1 = self.fig1.add_subplot(223)
        self.ax1.set_axis_off()
        self.ax1.imshow(Glbytergb)
        # 提取蓝色通道
        blue_data = np.zeros_like(raw_data)
        blue_data[1::2, ::2] = raw_data[1::2, ::2]
        Bbyte_r = np.zeros_like(blue_data)
        Bbyte_g = np.zeros_like(blue_data)
        Bbyte_b = blue_data.astype('uint8')
        Bbytergb = np.stack([Bbyte_r, Bbyte_g, Bbyte_b], axis=-1)
        self.ax1 = self.fig1.add_subplot(224)
        self.ax1.set_axis_off()
        self.ax1.imshow(Bbytergb)

        self.vieWidget.draw()

    def view_4_function(self, raw_data):
        # 提取的0/S0/16为R通道图像
        s0_pickup = raw_data[:, 0::4]
        s0_pickup_reshape = s0_pickup.reshape((1024, 1224))
        # 提取的45/S1/16为G通道图像
        s1_pickup = raw_data[:, 1::4]
        s1_pickup_reshape = s1_pickup.reshape((1024, 1224))
        # 提取的90/S2/16为G通道图像
        s2_pickup = raw_data[:, 2::4]
        s2_pickup_reshape = s2_pickup.reshape((1024, 1224))
        # 提取的135/S3/16为B通道图像
        s3_pickup = raw_data[:, 3::4]
        s3_pickup_reshape = s3_pickup.reshape((1024, 1224))
        if 'RG16' not in self.rawData:
            print(f'pixel format is 0-45-90-135 S0-S1-S2-S3')
            self.ax1 = self.fig1.add_subplot(221)
            self.ax1.set_aspect('equal')
            self.ax1.set_axis_off()
            self.ax1.imshow(s0_pickup_reshape, cmap='gray')
            time.sleep(0.1)
            self.ax1 = self.fig1.add_subplot(222)
            self.ax1.set_axis_off()
            self.ax1.imshow(s1_pickup_reshape, cmap='gray')
            time.sleep(0.1)
            self.ax1 = self.fig1.add_subplot(223)
            self.ax1.set_axis_off()
            self.ax1.imshow(s2_pickup_reshape, cmap='gray')
            time.sleep(0.1)
            self.ax1 = self.fig1.add_subplot(224)
            self.ax1.set_axis_off()
            self.ax1.imshow(s3_pickup_reshape, cmap='gray')
            time.sleep(0.1)
            self.vieWidget.draw()
        else:
            print(f'pixel format is 16bits')
            # 提取红色通道
            Rbyte_r = s0_pickup_reshape.astype('uint8')
            Rbyte_g = np.zeros_like(s0_pickup_reshape)
            Rbyte_b = np.zeros_like(s0_pickup_reshape)
            Rbytergb = np.stack([Rbyte_r, Rbyte_g, Rbyte_b], axis=-1)
            self.ax1 = self.fig1.add_subplot(221)
            self.ax1.set_aspect('equal')
            self.ax1.set_axis_off()
            self.ax1.imshow(Rbytergb)
            # 提取绿色通道
            Grbyte_r = np.zeros_like(s1_pickup_reshape)
            Grbyte_g = s1_pickup_reshape.astype('uint8')
            Grbyte_b = np.zeros_like(s1_pickup_reshape)
            Grbytergb = np.stack([Grbyte_r, Grbyte_g, Grbyte_b], axis=-1)
            self.ax1 = self.fig1.add_subplot(222)
            self.ax1.set_axis_off()
            self.ax1.imshow(Grbytergb)

            Glbyte_r = np.zeros_like(s2_pickup_reshape)
            Glbyte_g = s2_pickup_reshape.astype('uint8')
            Glbyte_b = np.zeros_like(s2_pickup_reshape)
            Glbytergb = np.stack([Glbyte_r, Glbyte_g, Glbyte_b], axis=-1)
            self.ax1 = self.fig1.add_subplot(223)
            self.ax1.set_axis_off()
            self.ax1.imshow(Glbytergb)
            # 提取蓝色通道
            Bbyte_r = np.zeros_like(s3_pickup_reshape)
            Bbyte_g = np.zeros_like(s3_pickup_reshape)
            Bbyte_b = s3_pickup_reshape.astype('uint8')
            Bbytergb = np.stack([Bbyte_r, Bbyte_g, Bbyte_b], axis=-1)
            self.ax1 = self.fig1.add_subplot(224)
            self.ax1.set_axis_off()
            self.ax1.imshow(Bbytergb)

            self.vieWidget.draw()
